package com.roncoo.education.db.service;

import com.roncoo.education.api.request.ProductImageRequest;
import com.roncoo.education.db.dao.ProductImageDao;
import com.roncoo.education.db.entity.ProductImage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProductImageService {

    @Resource
    ProductImageDao productImageDao;

    public void saveImage(Long proId,List<ProductImageRequest> list){
        ProductImage deletBean = new ProductImage();
        deletBean.setProductId(proId);
        productImageDao.deleteByBean(deletBean);
        for (ProductImageRequest productImage : list) {
            ProductImage image = new ProductImage();
            image.setProductId(proId);
            image.setCreateTime(new Date());
            image.setImageUrl(productImage.getImageUrl());
            image.setIfMain(productImage.getIfMain());
            productImageDao.insert(image);
        }
    }

    public List<ProductImage> getImagesByProId(Long proId){
        ProductImage query = new ProductImage();
        query.setProductId(proId);
        List<ProductImage> list = productImageDao.findByBean(query);
        return list;
    }
}
